Primary exercises
- A function with
if/elsestatements.
Let’s take the following from a World Health Organization document:
- An
adultis a person older than 19 years of age unless national law defines a person as being an adult at an earlier age. - An
adolescentis a person aged 10 to 19 years inclusive. - A
childis a person 19 years or younger unless national law defines a person to be an adult at an earlier age. - However, in these guidelines when a person falls into the 10 to 19 age category they are referred to as an adolescent (see adolescent definition).
- An
infantis a child younger than one year of age.
The categories can be enumerated as follows:
| group | condition for age [y] |
|---|---|
adult |
age > 19 |
adolescent |
age >= 10 && age <= 19 |
child |
age >= 1 && age < 10 |
infant |
age < 1 |
Implement a function ageGroup which takes age argument (in years) and returns the age group label.
Function template Here is a template of the ageGroup function:
ageGroup <- function( age ) {
# age: age given in years
...
}
and here are expected results:
ageGroup(0) # "infant"
ageGroup(10) # "adolescent"
ageGroup(9) # "child"
ageGroup(20) # "adult"
ageGroup(-1) # should generate an error
To generate the error we request the program to ?stop with an error message.